const router = require('koa-router')()
const { addLunbo, getLunbo, deleteLunbo ,addDetail, getDetail, deleteDetail, updateDetail} = require('../dao/jinguanDao')


router.post('/uploadLunbo', async (ctx) => {
    console.log('123123123123',ctx.request.files.file)
    const imgPath = ctx.request.files.file.name
    console.log(imgPath)
    await addLunbo(imgPath);
	return ctx.body = '上传成功';
})

router.get('/getLunbo', async (ctx) => {
    const data =await getLunbo()
    return ctx.body = data;
})

router.post('/deleteLunbo', async (ctx) => {
    const data = ctx.request.body;
    console.log(data);
    const deleteData = await deleteLunbo(data)
    if(deleteData['ok'] === 1){
        return ctx.body = {code: 200, msg:'删除成功'}
    }else {
        return ctx.body = {code: 400, msg:'删除失败'}
    }
})
//删除详情
router.post('/deleteDetail', async (ctx) => {
    const data = ctx.request.body;
    console.log(data);
    const deleteData = await deleteDetail(data)
    if(deleteData['ok'] === 1){
        return ctx.body = {code: 200, msg:'删除成功'}
    }else {
        return ctx.body = {code: 400, msg:'删除失败'}
    }
})
//上传图片
router.post('/uploadDetailImg', async (ctx) => {
    console.log('详情',ctx.request.files.file)
    const imgName = ctx.request.files.file.name
    return ctx.body = {imgName:imgName,code:200}
})
//新增详情
router.post('/uploadDetail', async (ctx) => {
    const data = ctx.request.body;
    console.log(data);
    const model = await addDetail(data)
    if(model['_id']){
        return ctx.body = {imgName:'上传成功',code:200};
    }else{
        return ctx.body = {imgName:'上传失败',code:400};
    }
})
//获得详情
router.get('/getDetail', async (ctx) => {
    const data = await getDetail(ctx.request.query)
    return ctx.body = data;
})

router.post('/updateDetail', async (ctx) => {
    const data = ctx.request.body;
    console.log(data);
    const model = await updateDetail(data)
    console.log(model)
    if(model['_id']){
        return ctx.body = {imgName:'修改成功',code:200};
    }else{
        return ctx.body = {imgName:'修改失败',code:400};
    }

})

module.exports = router
